/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controle;

import Modelo.Sala;
import Modelo.TableModelSala;
import Visao.CinemaGUI;

/**
 *
 * @author andreus & gabriel
 */
public class SalaController {

    private CinemaGUI view;
    private TableModelSala model;

    public SalaController(CinemaGUI view, TableModelSala model) {
        this.view = view;
        this.model = model;
    }

    public void remove() {
        int index = view.getTableSalas().getSelectedRow();
        if (index == -1) {
            return;
        }
        model.remove(index);
        view.getCmbSala().removeItemAt(index);
    }
    
    public void insert(){
        Sala s = newFromView();
        if(s != null && model.search(s.getNumero()) == null){
            model.add(s);
            view.getCmbSala().addItem(s.getNumero());
        }
    }
    
    public void select(){
        int index = view.getTableSalas().getSelectedRow();
        if (index == -1){
            return;
        }
        Sala s = model.select(index);
        view.getTxtNroSala().setText(""+s.getNumero());
        view.getTxtLotacao().setText(""+s.getLotacao());
    }

    private Sala newFromView() {
        try {
            Sala sala = new Sala();
            sala.setNumero(Integer.parseInt(view.getTxtNroSala().getText()));
            sala.setLotacao(Integer.parseInt(view.getTxtLotacao().getText()));
            return sala;
        } catch (Exception e) {

            return null;
        }
    }
}
